﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SurvivalStrategy.Graphics;
using Microsoft.Xna.Framework.Graphics;

namespace SurvivalStrategy
{
    //klasa odpowiedzialna za efekt wizualny
    public class View
    {
        private GraphicQueue graphicQueue;
        private List<GraphicElement> highLevel;
        private List<GraphicElement> midLevel;
        private List<GraphicElement> lowLevel;

        private Dictionary<int, Texture2D> textures;
        private static List<DebugImage> debugList = new List<DebugImage>();     //lista obrazkow sluzacych do debugu, z dowolnego miejsca mozemy sie do niej odwolac i dodac testowy draw
        private SpriteBatch spriteBatch;

        public View()
        {
            graphicQueue = new GraphicQueue();
            highLevel = new List<GraphicElement>();
            midLevel = new List<GraphicElement>();
            lowLevel = new List<GraphicElement>();
            textures = new Dictionary<int,Texture2D>();
        }

        public void SetRender(SpriteBatch spriteBatch)
        {
            this.spriteBatch = spriteBatch;
        }

        public void Draw(GameTime gameTime)
        {
            DrawWorld();
            DrawGUI();
           //  DrawDebugTests();
        }

        private void DrawWorld()
        {
            /*while(graphicQueue.Count > 0)
            {
                GraphicElement el = graphicQueue.Dequeue();

                spriteBatch.Draw(textures[el.TextureID], new Vector2(el.Position.X, el.Position.Z), Color.White);
            }*/
            foreach(GraphicElement el in lowLevel)
                spriteBatch.Draw(textures[el.TextureID], new Vector2(el.Position.X, el.Position.Z), Color.White);
            foreach (GraphicElement el in midLevel)
                spriteBatch.Draw(textures[el.TextureID], new Vector2(el.Position.X, el.Position.Z), Color.White);
            foreach (GraphicElement el in highLevel)
                spriteBatch.Draw(textures[el.TextureID], new Vector2(el.Position.X, el.Position.Z), Color.White);
            lowLevel.Clear();
            midLevel.Clear();
            highLevel.Clear();
        }

        private void DrawDebugTests()
        {
            for(int i = 0; i<debugList.Count; i++)
            {
                spriteBatch.Draw(textures[debugList[i].Texture], debugList[i].Position, Color.White);
            }
            debugList.Clear();
        }

        public static void DrawDebugImage(Vector2 pos, int tex)
        {
            //debugList.Add(new DebugImage(pos, tex));
        }

        private void DrawGUI()
        {
        }

        public void SetGraphicsData(List<GraphicElement> data)
        {
            //this.graphicQueue = data;
            foreach (GraphicElement g in data)
            {
                if (g.Position.Y > 0)
                    highLevel.Add(g);
                else if (g.Position.Y == 0)
                    midLevel.Add(g);
                else lowLevel.Add(g);

            }
        }

        public void AddTexture(int id, Texture2D texture)
        {
            textures.Add(id, texture);
        }
    }

    public struct DebugImage
    {
        public Vector2 Position;
        public int Texture;

        public DebugImage(Vector2 pos, int tex)
        {
            Position = pos;
            Texture = tex;
        }
    }

}
